package com.how2java.springboot.config;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Wan Yucan
 * @date 2020/12/1-17:20
 */
@Component
public class TestSocket implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception{
        ServerSocket server=new ServerSocket(10086);
        System.out.println("服务器已启动，正在监听10086");
        while (true) {
            try {
                Socket socket = server.accept();
//            Thread.sleep(1000);
                byte[] inBytes = new byte[14];
                InputStream input = socket.getInputStream();
                input.read(inBytes, 0, 14);
                Thread.sleep(500);
                System.out.println("Client:" + new String(inBytes));
                socket.getOutputStream().write(inBytes);
                System.out.println("Server:" + new String(inBytes));
                socket.close(); //关闭Socket
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
